# in root directory run:
# prove -v t/t1-junc-pos.t
use strict;
use warnings;
use Data::Dumper;
use lib '/Users/manuelcorpas/Documents/workspace/Junction_filter' ;
use Modules::Utilities;
use Modules::JunctionRW;
use Modules::JunctionFilter;

use Test::More tests => 35;

BEGIN { 
  use_ok('Modules::Utilities'); 
  use_ok('Modules::JunctionRW');
  use_ok('Modules::JunctionFilter');
}
my $root      = '/Users/manuelcorpas/Documents/workspace/Junction_filter' ;
my $conf_file = "conf/test_conf4.txt";
my $util      = Modules::Utilities->new;

ok( defined $util, 'Utilities returned something' );
 
$util->read_conf_file("$root/$conf_file");
my $filename  = $util->{DATA_PATH}.$util->{SAM_FILE};

ok( -e $filename, "SAM file exists" );


my $jrw       = Modules::JunctionRW->new($util);

ok( defined $jrw, 'Junction read write returned something' );

my $sam       = $jrw->parse_sam;

ok( defined $sam, 'SAM parser returned something' );
ok( -e "$filename.unspliced_reads", "Unspliced_reads file exists" );
ok( -e "$filename.spliced_reads_info", "Spliced_reads_info file exists" );
ok( -e "$filename.spliced_reads", "Spliced_reads file exists" );

my $filter    = Modules::JunctionFilter->new($util);

ok( defined $filter, 'filter obj returned something' );

my $junctions = $filter->calc_junctions($sam);

$filter->calc_junc_read_redundancy;

ok( defined $junctions, 'junctions array returned something' );

=comment
my @juncs     = keys %$junctions;
map {print "$_\t".$junctions->{$_}{support    }    ."\t";
     print        $junctions->{$_}{left_flank }[0 ]."\t";
     print        $junctions->{$_}{right_flank}[-1]."\t";
     print        $junctions->{$_}{junc_start }    ."\t";
     print        $junctions->{$_}{junc_end   }    ."\n";
    }@juncs;
    
=cut

test_junction( 'Support + Position','23432_23586_Mp1087439_TGAC_V1_scaffold_344',$junctions,'-','3','23368','23432','23586','23632' );


$filter->apply_filters;

ok( -e "$filename.fail.gff", "Filtered out junction file exists" );
ok( -e "$filename.pass.gff", "Spliced_reads_info file exists" );


sub test_junction {
  my ($test_name,$key,$j,$t_strand,$t_support,$t_left_flank,$t_junc_start,$t_junc_end,$t_right_flank) = @_;
  
  print "\nJUNC TEST: $test_name\n";
  my $junc         = $j->{$key           };
  my $support      = $junc->{support     };
  my $left_flank   = $junc->{left_flank  }[0 ];
  my $junc_start   = $junc->{junc_start  };
  my $junc_end     = $junc->{junc_end    };
  my $right_flank  = $junc->{right_flank }[-1];
  my $strand       = $junc->{query_strand};
  print "No. reads w/ mismatches:\t" . $junc->{no_mismatches} . "\n";
  ok( exists $junctions->{$key}, "junc $test_name exists" );
  ok( $strand      eq $t_strand,      "query strand"      );
  is( $support,       $t_support,     "support"           );
  is( $left_flank,    $t_left_flank,  "left flank start"  );
  is( $junc_start,    $t_junc_start,  "junc start"        );
  is( $junc_end,      $t_junc_end,    "junc end"          );
  is( $right_flank,   $t_right_flank, "right flank end"   );
}